﻿using Application.Services;
using System.Web.Mvc;
using Web.Infrastructure.Mvc.Authentication;
using Web.Models;

namespace Web.Controllers
{
    [Authorize]
    public class AccountController : Controller
    {
        private IFormsAuthentication _formsAuthentication;
        private IAccountService _accountService;

        public AccountController(IFormsAuthentication formsAuthentication, IAccountService accountService)
        {
            _formsAuthentication = formsAuthentication;
            _accountService = accountService;
        }

        public ActionResult Index()
        {
            return View();
        }

        [AllowAnonymous]
        public ActionResult LoginPartial()
        {
            return PartialView();
        }

        [AllowAnonymous]
        public ActionResult Login()
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Index");
            }
            return View(new LoginModel());
        }

        [HttpPost, AllowAnonymous, ValidateAntiForgeryToken]
        public ActionResult Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                var result = _accountService.ValidationCustomer(model.UserName, model.Password);
                if (!result)
                {
                    ModelState.AddModelError("", "用户名或密码错误");
                }
                else
                {
                    _formsAuthentication.SignIn(model.UserName, false);
                    return RedirectToAction("Index");
                }
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Logout()
        {
            _formsAuthentication.SignOut();
            return RedirectToAction("Login");
        }

        [AllowAnonymous]
        public ActionResult Register()
        {
            return View(new RegisterModel());
        }

        [HttpPost, AllowAnonymous, ValidateAntiForgeryToken]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                _accountService.CreateCustomer(model.UserName, model.Password);
                _formsAuthentication.SignIn(model.UserName, false);
                return RedirectToAction("Index");
            }
            return View(model);
        }

        public ActionResult ChangePassword()
        {
            return View(new ChangePasswordModel());
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            if (ModelState.IsValid)
            {
                var result = _accountService.ChangePassword(model.OldPassword, model.NewPassword);
                if (!result)
                {
                    ModelState.AddModelError("OldPassword", "原始密码错误");
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            return View(model);
        }
    }
}